package com.perfect.controller;

import com.perfect.base.BaseController;
import com.perfect.base.ResultInfo;
import com.perfect.query.UserQuery;
import com.perfect.service.UserService;
import com.perfect.utils.AssertUtil;
import com.perfect.vo.Product;
import com.perfect.vo.Shop;
import com.perfect.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public ResultInfo login(String username, String password,HttpServletRequest request){
//        request.setAttribute("user1",userService.getUser(username));

        ResultInfo resultInfo = userService.login(username,password);
//        String id="";
//        Cookie[] cookies = request.getCookies();
//
//        for (Cookie c : cookies){
//            if (c.getName().equals("id")){
//                id = c.getValue();
//                break;
//            }
//        }
        return resultInfo;
    }


    /**
     * 进入修改密码页面
     * @return
     */
    @RequestMapping("toPasswordPage")
    public String toPasswordPage() {

        return "user/password";
    }

    /**
     * 进入人员管理的管理员列表
     * @return
     */
    @RequestMapping("admin/index")
    public String toAdminList() {

        return "user/admin_list";
    }

    /**
     * 进入人员管理的管理员列表
     * @return
     */
    @RequestMapping("merchant/index")
    public String toMerchantList() {

        return "user/merchant_list";
    }

    /**
     * 条件查询普通管理员
     * @return
     */
    @RequestMapping("/admin/list")
    @ResponseBody
    public Map<String,Object> listAdmin(UserQuery userQuery) {
        //
        userQuery.setRoleId(2);
        return userService.queryByParamsForTable(userQuery);
    }

    /**
     * 条件查询商家
     * @return
     */
    @RequestMapping("/merchant/list")
    @ResponseBody
    public Map<String,Object> listMerchant(UserQuery userQuery) {
        // 商家角色的id
        userQuery.setRoleId(3);
        return userService.queryByParamsForTable(userQuery);
    }

    /**
     * 条件查询商家
     * @return
     */
    @RequestMapping("/update_password")
    @ResponseBody
    public ResultInfo updatePassword(HttpServletRequest request,String oldPwd,String newPwd, String confirmPwd) {

        String id="";
        Cookie[] cookies = request.getCookies();
        for (Cookie c : cookies){
            if (c.getName().equals("id")){
                id = c.getValue();
                break;
            }
        }

        return userService.updatePassword(id,oldPwd,newPwd,confirmPwd);
    }



    /**
     * 进入人员管理的管理员列表
     * @return
     */
    @RequestMapping("to_setting_page")
    public String toSettingPage(HttpServletRequest request) {

//        String id="";
//        Cookie[] cookies = request.getCookies();
//        for (Cookie c : cookies){
//            if (c.getName().equals("id")){
//                id = c.getValue();
//                break;
//            }
//        }
//
//        User user = userService.getUserById(id);
//        request.setAttribute("user",user);
//        System.out.println(user);


        return "user/personal_information";
    }


    /**
     * 进入人员管理的管理员列表
     * @return
     */
    @RequestMapping("get_login")
    @ResponseBody
    public ResultInfo getLogin(HttpServletRequest request) {

        ResultInfo resultInfo = new ResultInfo();

        String id="";
        Cookie[] cookies = request.getCookies();
        for (Cookie c : cookies){
            if (c.getName().equals("id")){
                id = c.getValue();
                break;
            }
        }

        User user = userService.getUserById(id);
        request.setAttribute("user",user);
//        System.out.println(user);

        AssertUtil.isTrue(null==user,"未登录");
        resultInfo.setCode(200);
        resultInfo.setResult(user);

        return resultInfo;
    }



    @PostMapping("/upload_user_face")
//    @ResponseBody
    public void uploadUserFace(HttpServletRequest request) {

//        // 获取上传文件的目录
//        String uploadFilePath = request.getServletContext().getRealPath("/images/user/");
//        System.out.println("uploadFilePath==="+uploadFilePath);
        // F:\msbproject3\course\mall\src\main\resources
        userService.uploadImg(request);
    }



    @RequestMapping("toAddOrUpdateUserPage")
    public String toAddOrUpdateUserPage(Integer roleId, Integer id, HttpServletRequest request) {

        // 判断id是否为空，不为空表示更新操作，查询用户对象
        if (id != null) {
            // 通过id查询用户对象
            User user = userService.selectByPrimaryKey(id);
//            System.out.println("user===="+user);
            user.setPassword(null);
            // 将数据设置到请求域中
            request.setAttribute("userInfo",user);
        }


        if (roleId != null) {
            // 将数据设置到请求域中
            request.setAttribute("roleId",roleId);
        }

        return "user/add_update";
    }


    @PostMapping("save")
    @ResponseBody
    public ResultInfo saveUser(Integer roleId, User user) {

        userService.saveUser(roleId,user);
        return success("用户添加成功！");
    }

    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateUser(User user) {
        userService.updateUser(user);
        return success("用户更新成功！");
    }

    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteUser(Integer[] ids) {

        userService.deleteByIds(ids);

        return success("用户删除成功！");
    }


    @RequestMapping("/toDetailPage")
    public String toDetailPagePage(Integer id, HttpServletRequest request) {

        // 通过传过来的用户id查询改用户的店铺
        Shop shop = userService.getShopByUserId(id);
        request.setAttribute("shop",shop);

        return "user/detail";
    }


}
